package com.duobaoyu.dby.supply.admin.controller.message;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.admin.converter.SupplyChainAdminConverter;
import com.duobaoyu.dby.supply.biz.service.message.MessageService;
import com.duobaoyu.dby.supply.common.biz.dto.message.DelayMessageDTO;
import com.duobaoyu.dby.supply.common.biz.dto.message.DelayMessageStatusDTO;
import com.duobaoyu.dby.supply.common.biz.vo.message.DelayMessageVO;
import com.duobaoyu.dby.supply.db.entity.message.DelayMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName DelayMessageController
 * @Description 延时消息Controller
 * @createTime 15:24:00 2023/11/08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/delay-message")
@Api(value = "delay-message", tags = "延时消息")
public class DelayMessageController {

    private final MessageService messageService;
    private final SupplyChainAdminConverter supplyChainAdminConverter;

    /**
     * 分页查询延时消息信息
     *
     * @param delayMessage
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询延时消息信息", notes = "分页查询延时消息信息")
    @SupplySysLog(value = "分页查询延时消息信息", operationType = OperationTypeEnum.SELECT)
    public R<Page<DelayMessageVO>> delayMessagePage(DelayMessageDTO delayMessage) {
        Page<DelayMessage> page = messageService.delayMessagePage(delayMessage);
        return R.ok(supplyChainAdminConverter.toDelayMessagePage(page));
    }

    /**
     * 修改延时消息状态
     * @param delayMessageStatus
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改延时消息状态", notes = "修改延时消息状态")
    @SupplySysLog(value = "修改延时消息状态",operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> updateChannelMessageStatus(@Validated @RequestBody DelayMessageStatusDTO delayMessageStatus) {
        Boolean result = messageService.updateDelayMessageStatus(delayMessageStatus);
        return R.ok(result);
    }

    /**
     * 延时消息手动执行业务逻辑
     * @param delayMessageId
     * @return
     */
    @PutMapping("/handler/{delayMessageId}")
    @ApiOperation(value = "延时消息手动执行业务逻辑", notes = "延时消息手动执行业务逻辑")
    @SupplySysLog(value = "延时消息手动执行业务逻辑",operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> delayMessageHandler(@NotBlank(message = "渠道消息Id不能为空") @PathVariable("delayMessageId") String delayMessageId) {
        messageService.delayMessageHandler(delayMessageId);
        return R.ok(true, "操作成功！");
    }
}
